import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  ClassSerializerInterceptor,
  UseInterceptors,
  Query,
  UseGuards,
} from '@nestjs/common';
import { DepartmentService } from './department.service';
import { CreateDepartmentDto } from './dto/create-department.dto';
import { UpdateDepartmentDto } from './dto/update-department.dto';
import {
  ApiBody,
  ApiOperation,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { ReturnDepartmentDtoList } from './dto/get-department.dto';
import { JwtGuard } from '../guards/jwt.guards';

@Controller('department')
@UseInterceptors(ClassSerializerInterceptor)
@UseGuards(JwtGuard) // 校验token是否通过
@ApiTags('学院模块')
export class DepartmentController {
  constructor(private readonly departmentService: DepartmentService) {}

  @ApiOperation({ summary: '查询学院下面的专业' })
  @ApiQuery({ name: 'id', required: true })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: ReturnDepartmentDtoList,
  })
  @Get('/subordinate')
  findSubordinate(@Query('id') id: number) {
    return this.departmentService.findSubordinate(id);
  }

  @Post()
  @ApiOperation({ summary: '创建学院' })
  @ApiBody({ type: CreateDepartmentDto, description: '' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreateDepartmentDto,
  })
  create(@Body() createDepartmentDto: CreateDepartmentDto) {
    return this.departmentService.create(createDepartmentDto);
  }

  @Get()
  @ApiOperation({ summary: '查询所有学院' })
  findAll() {
    return this.departmentService.findAll();
  }

  @Get(':id')
  @ApiOperation({ summary: '查询学院详情' })
  @ApiQuery({ name: 'id', required: true })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreateDepartmentDto,
  })
  findOne(@Param('id') id: number) {
    return this.departmentService.findOne(id);
  }

  @ApiOperation({ summary: '学院更新' })
  @ApiQuery({ name: 'id', required: true, description: '学院id' })
  @ApiBody({ type: CreateDepartmentDto, description: '' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreateDepartmentDto,
  })
  @Patch(':id')
  update(
    @Param('id') id: number,
    @Body() updateDepartmentDto: UpdateDepartmentDto,
  ) {
    return this.departmentService.update(id, updateDepartmentDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '学院删除' })
  @ApiQuery({ name: 'id', required: true, description: '学院id' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreateDepartmentDto,
  })
  remove(@Param('id') id: number) {
    return this.departmentService.remove(id);
  }
}
